﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using StarryEdge.Core.Enums;
using StarryEdge.Core.Models;
using StarryEdge.Core.Parameter;
using StarryEdge.Menu.User.ViewModels.Dialog;
using StarryEdge.Menu.User.Views.Dialog;
using StarryEdge.WPF.Command;
using StarryEdge.WPF.Tool;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace StarryEdge.Menu.User.ViewModels
{
    public class RoleEditViewModel
    {
        public ObservableCollection<RoleModel> Roles { get; set; }

        private IUserEntity _userEentity;
        public RoleEditViewModel(IUserEntity userEentity)
        {
            _userEentity = userEentity;
            Roles = new ObservableCollection<RoleModel>();
            foreach (RoleModel role in _userEentity.GetRoles())
            {
                Roles.Add(role);
            }
        }

        public DelegateCommand New => new DelegateCommand(async () =>
        {
            await DialogHost.Show(new AddRole() { DataContext = new AddRolePopupDialog(_userEentity, default) }, PopUpTypes.Global.ToString());
            Roles.Clear();
            foreach (RoleModel role in _userEentity.GetRoles())
            {
                Roles.Add(role);
            }
        });

        public DelegateCommand<ListView> SelectDelete => new DelegateCommand<ListView>(sender => 
        {
            foreach (var item in sender.SelectedItems)
            {
                _userEentity.RemoveRole(item as RoleModel);
            }
            _userEentity.Save();
            Roles.Clear();
            foreach (RoleModel role in _userEentity.GetRoles())
            {
                Roles.Add(role);
            }
        });

        public DelegateCommand<RoleModel> MouseDoubleClick => new DelegateCommand<RoleModel>(sender =>
        {
            if (sender != null)
            {
                _ = DialogHost.Show(new AddRole() { DataContext = new AddRolePopupDialog(_userEentity, sender) }, PopUpTypes.Global.ToString());
            }
        });
    }
}
